using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LevelData;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace GGJ11
{
    class RenderingEnemies : RenderingPlayer
    {
        Level level;
        Texture2D life;

        public RenderingEnemies() { }
        public RenderingEnemies(ContentManager Content)
            : base(Content)
        {
            level = Content.ServiceProvider.GetService(typeof(Level)) as Level;
            life = Content.Load<Texture2D>("Sprites/MonsterA/life");
        }

        //draw enemies
        public void Draw(GameTime gameTime, SpriteBatch sp, Vector2 visibleHArea, Vector2 visibleVArea)
        {
            SpriteEffects flip;
            for (int i = 0; i < level.enemyList.Count; i++)
            {
                if (!level.enemyList[i].die)
                {
                    if (level.enemyList[i].direction == FaceDirection.Right)
                    {
                        flip = SpriteEffects.FlipHorizontally;
                    }
                    else
                    {
                        flip = SpriteEffects.None;
                    }
                }
                else
                {
                    flip = SpriteEffects.None;
                }

                if ((level.enemyList[i].position.X / Tile.Width) >= visibleHArea.X && (level.enemyList[i].position.X / Tile.Width) <= visibleHArea.Y)
                {
                    if ((level.enemyList[i].position.Y / Tile.Height) >= visibleVArea.X && (level.enemyList[i].position.Y / Tile.Height) <= visibleVArea.Y)
                    {
                        if (level.enemyList[i].type == EnemyType.Monster)
                        {
                            Vector2 positionEnemy = level.enemyList[i].position - Vector2.UnitY * Tile.Height;
                            level.enemyList[i].sprite.Draw(gameTime, sp, positionEnemy, flip);
                            for (int lifeIndex = 0; lifeIndex <  level.enemyList[i].health; lifeIndex++)
                            {
                                sp.Draw(life, new Rectangle((int)positionEnemy.X + (level.enemyList[i].localBounds.Width/2) + lifeIndex*10, (int)positionEnemy.Y, 10,10), Color.White);
                            }
                        }
                        else
                        {
                            level.enemyList[i].sprite.Draw(gameTime, sp, level.enemyList[i].position, flip);
                        }
                    }
                }
            }
            base.Draw(gameTime, sp);
        }

        //update service when load a next level
        public void UpdateLevelService(ContentManager content)
        {
            level = content.ServiceProvider.GetService(typeof(Level)) as Level;
            base.UpdateLevelService(content);

        }
    }
}
